package com.helloandroid.dialogs

import android.view.*
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.helloandroid.AppUtil
import com.helloandroid.MyApplication
import com.helloandroid.R
import com.helloandroid.activitys.ChiFanActivity
import com.helloandroid.base.BaseDialog
import com.helloandroid.tools.MyLog
import com.helloandroid.tools.MyTimeUtils

enum class AwardState(val value:Int){
    Geted(1), // 已经领取
    Getable(2), // 可以领取,但是还没有领取
    SupplementGetable(3), // 可以补领
    TimeNotYet(4), // 时间未到,不可以领取
}

class ViewHolder(
        private val rootView:View,
        val tvTitle:TextView,
        val tvTime:TextView,
        val ivCoin:ImageView,
        val tvCoin:TextView
                 ): RecyclerView.ViewHolder(rootView)

class Adapter(private val cfActivity:ChiFanActivity): RecyclerView.Adapter<ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val rootView = LayoutInflater.from(parent.context).inflate(R.layout.dialog_zaocan_item,parent,false)
        return ViewHolder(rootView,
                rootView.findViewById(R.id.tvTitle),
                rootView.findViewById(R.id.tvTime),
                rootView.findViewById(R.id.ivCoin),
                rootView.findViewById(R.id.tvCoin)
                )
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = ChiFanActivity.dataLst[position]
        holder.tvTitle.text = item.title
        holder.tvTime.text = item.timeSegStr
        holder.ivCoin.setImageResource(R.drawable.dd_coin_small)
        holder.tvCoin.text =  "+${item.coin}"

        val btn = holder.itemView.findViewById<Button>(R.id.btn)
        val state = cfActivity.mInfo!!.getValueByChiFanId(position + 1)
        var awardState:AwardState
        if (state > 0){
            // 已领取
            btn.text = "已领取"
            btn.setBackgroundResource(R.drawable.dd_btn_gray)
            awardState = AwardState.Geted
        }else{
            // 1. 补领
            // 2. 待领取
            // 3. 领取
            val sysTime = AppUtil.sysTime
            val offsetToday = MyTimeUtils.getTodayOffsetMs(sysTime)
            if(offsetToday < item.startOffset){
                // 待领取
                btn.text = "待领取"
                btn.setBackgroundResource(R.drawable.btn_blue)
                awardState = AwardState.TimeNotYet
            }else if(offsetToday > item.endOffset){
                btn.text = "补领"
                btn.setBackgroundResource(R.drawable.btn_red)
                awardState = AwardState.SupplementGetable
            }else{
                // 领取
                btn.text = "领取"
                btn.setBackgroundResource(R.drawable.btn_red)
                awardState = AwardState.Getable
            }
        }

        if(awardState == AwardState.TimeNotYet ||
                awardState == AwardState.Geted){
            btn.setOnClickListener(null)
        }else{
            //  领取 or 补充领
            btn.setOnClickListener {
                MyLog.elog("领取餐补....${item.title}")

                // 这里打完卡 需要刷新界面
                cfActivity.daka(item){
                    // 刷新列表
                    notifyDataSetChanged()
                }
            }
        }
    }

    override fun getItemCount(): Int {
        return ChiFanActivity.dataLst.size
    }
}

class ZaoCanDialog(private val cfActivity:ChiFanActivity) :BaseDialog(cfActivity) {
    lateinit var adapter:Adapter

    override fun getContentViewResId() = R.layout.dialog_zaocan

    override fun initView() {
        //
        val rvChiFan = findViewById<RecyclerView>(R.id.rvChiFan)
        adapter =  Adapter(cfActivity)
        rvChiFan.adapter = adapter

        findBtn(R.id.btnClose).setOnClickListener { cancel() }
    }

    override fun initData() {

    }
}